<?php
namespace app\index\model;
use think\Model;
class Brand extends Model
{
	protected $resultSetType='collection';
    public function index()
    {
        $data=model('Brand')->select();
        foreach ($data as $k)
        {
            $arr[]=$k->toArray();
        }
        return $arr;
    }
    public function addBrand()
    {
        if (isset($_POST['sub'])) {
            $file = request()->file('file');
            if (!empty($file)) {
                $time = date('Ymd', time());
                $filename = time() . 'file';
                if (!file_exists(ROOT_PATH . 'public' . DS . 'static' . DS . 'upload')) {
                    //创建文件
                    mkdir(ROOT_PATH . 'public' . DS . 'static' . DS . 'upload');
                }
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'upload' . DS . $time, $filename);
            }
            $brand = model('Brand')->insert
            ([
                'name' => input('firstname'),
                'logo' => $time . DS . $info->getFileName(),
                'txt' => input('text'),
            ]);
            return $brand;
        }
    }
        public function modBrand()
    {
        $bra=new Brand;
        if(isset($_POST['sub']))
        {
            $file=request()->file('file');
            if(!empty($file))
            {
                $id = trim(input('hid'));
                $data = Brand::get($id);
                $path=ROOT_PATH . 'public' . DS . 'static' . DS . 'upload'. DS .$data['logo'];
                if(file_exists($path)){
                    @unlink($path);
                    clearstatcache();
                }
                $time=date('Ymd',time());
                $filename=time().'file';
                $info=$file->move(ROOT_PATH . 'public' . DS .'static'. DS .'upload'.DS.$time,$filename);
                $rs=$bra->where('id',input('hid'))
                    ->update([
                        'name'     =>input('firstname'),
                        'logo'     =>$time.DS.$info->getFileName(),
                        'txt'      =>input('text'),
                    ]);
            }
            else
            {
                $rs=$bra->where('id',input('hid'))
                    ->update
                    ([
                        'name'     =>input('firstname'),
                        'txt'      =>input('text'),
                    ]);
            }
            return $rs;
        }

    }
}